//
//  YygCanViewController.m
//  ShangChengMoBan
//
//  Created by 军 on 16/8/27.
//  Copyright © 2016年 ping. All rights reserved.
//一元购参与记录

#import "YygCanViewController.h"
#import "CanYuTableViewCell.h"
@interface YygCanViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation YygCanViewController
{
    int _page;
    UIView * _view,*_homeView;
    RWScrollView * _table;
    NSMutableArray * _arr;
    NSDictionary * _dic;
}
-(void)headRefresh{
    _page = 1;
    [self RequestData:1];
}
-(void)footerRefresh{
    _page ++;
    [self RequestData:0];
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
     _page = 1;
     _table =[[RWScrollView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-104)];
    [_table addHeaderWithTarget:self action:@selector(headRefresh)];
    
    [_table addFooterWithTarget:self action:@selector(footerRefresh)];
    _table.delegate =self;
    _table.dataSource = self;
    _table.separatorStyle = UITableViewCellSelectionStyleNone;
    [_table registerClass:[CanYuTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_table];
    [_table headerBeginRefreshing];
  
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arr.count;
}
-(void)RequestData:(int)first{
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"10" forKey:@"pageSize"];
    [dic setObject:[NSString stringWithFormat:@"%d",_page] forKey:@"pageNum"];
    [dic setObject:[self defaultUserId] forKey:@"userId"];
    [dic setObject:[self defaultLoadUserName] forKey:@"userName"];
    [dic setObject:@0 forKey:@"type"];

    [NetWorking NEWjieKouQingQiu:YYG_myYYG ANDIP:IP And:dic anBlock:^(NSDictionary *dic, NSError *error) {
        [_table headerEndRefreshing];
        [_table footerEndRefreshing];

        if (!error) {
            if ([dic[Code] intValue] == 200) {
                if (first == 1) {
                    _arr =  [[NSMutableArray alloc]init];
                }
                [_arr addObjectsFromArray:dic[Response]];
                [_table reloadData];
            }
        }
    }];
}
-(void)tanKuang{
    _homeView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
    _homeView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
    [[UIApplication sharedApplication].keyWindow addSubview:_homeView];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    [_homeView addGestureRecognizer:tap];
    _view = [[UIView alloc]init];
    _view.layer.cornerRadius = 7;
    _view.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.93 alpha:1.00];
    [_homeView addSubview:_view];
    UILabel * lable = [[UILabel alloc]initWithFrame:CGRectMake(8, 5, KScreenWidth-36, 35)];
    lable.numberOfLines = 2;//多行
    lable.text = _dic[@"goodsMapper"][@"goodsName"];
    lable.font = [UIFont systemFontOfSize:14];
    [_view addSubview:lable];
    UIView * xian1 = [[UIView alloc]initWithFrame:CGRectMake(10, lable.bottom+3, KScreenWidth-40, 1)];
    xian1.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.81 alpha:1.00];
    [_view addSubview:xian1];
    UILabel * tit = [[UILabel alloc]initWithFrame:CGRectMake(8,xian1.bottom+5, KScreenWidth-36, 21)];
      tit.textColor = MaintextColor;
    NSString * text = [NSString stringWithFormat:@"%@",_dic[@"goodsNum"]] ;
    NSMutableAttributedString * abc = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"参与 %@ 次, 夺宝号码",text]];
    [abc addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(3, text.length)];
    tit.attributedText = abc;
    tit.font = [UIFont systemFontOfSize:13];
    [_view addSubview:tit];
    UITextView * textv = [[UITextView alloc]initWithFrame:CGRectMake(8, tit.bottom+5, KScreenWidth-36, 130)];
    textv.textColor = MaintextColor;
    textv.editable = NO;
    textv.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.93 alpha:1.00];
    NSString * a = [NSString stringWithFormat:@"%@",_dic[@"allLuckNum"]];
   a = [a substringWithRange:NSMakeRange(1, a.length-2)];
    NSMutableString *str1 = [NSMutableString stringWithString:a];
     for (int i = 0; i < str1.length; i++) {
        unichar c = [str1 characterAtIndex:i];
        NSRange range = NSMakeRange(i, 1);
        if ( c == ',') {
            [str1 replaceCharactersInRange:range withString:@" "];
            --i;
        }
    }
    [_view addSubview:textv];
     textv.text = [NSString stringWithString:str1];
   
    UIView * xian2 = [[UIView alloc]initWithFrame:CGRectMake(10, textv.bottom, KScreenWidth-40, 2)];
    xian2.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.81 alpha:1.00];
    [_view addSubview:xian2];

    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(0, xian2.bottom, KScreenWidth-20, 50)];
    [button setTitle:@"确定" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:13];
    [button addTarget:self action:@selector(queDing) forControlEvents:UIControlEventTouchUpInside];
    [_view addSubview:button];
    _view.frame = CGRectMake(10, 0, KScreenWidth-20, button.bottom);
    _view.center = self.view.center;
}
#pragma mark 余额弹框
-(void)tankuang:(UIButton *)button{
    _dic = _arr[button.tag];
    if (_homeView != nil) {
        [_homeView removeFromSuperview];
    }
    [self tanKuang];
 }
-(void)queDing{
    _homeView.hidden = YES;
}
-(void)tap:(UITapGestureRecognizer *)tap{
    _homeView.hidden = YES;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary * dic = _arr[indexPath.row];
    CanYuTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.title.textColor = MaintextColor;
    cell.chakan.tag = indexPath.row;
    [cell.chakan addTarget:self action:@selector(tankuang:) forControlEvents:UIControlEventTouchUpInside];
    cell.title.text = dic[@"goodsMapper"][@"goodsName"];//商品标题
    cell.num.text = [NSString stringWithFormat:@"%@",dic[@"goodsNum"]] ;//参与人次
    cell.qiShu.text =[NSString stringWithFormat:@"%@", dic[@"qishuNum"] ];//期数
    cell.zongRen.text = [NSString stringWithFormat:@"%@",dic[@"goodsAmount"]] ;//总人数
    [cell.image sd_setImageWithURL:[NSURL URLWithString:dic[@"goodsMapper"][@"goodsThumb"]]];
   

    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 125;
}


@end
